package cn.ranko.shiro.controller;

import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.utils.ConstantUtil;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zhujun on 2017/8/13.
 */
@Controller
@RequestMapping("/shiro/site")
public class SiteController {

    @RequiresPermissions("home")
    @RequestMapping("/index.action")
    public String index(ModelMap modelMap) {

        modelMap.addAttribute("menus", ConstantUtil.menus);

        return "site/index";
    }

    @RequestMapping("/login.action")
    @ResponseBody
    public String login(SysUser user, ModelMap modelMap) {

        String info = loginUser(user);
        if (!ApplicationCode.LOGIN_SUCCESS.getCode().equals(info)) {
            return JSON.toJSONString(new Message(ApplicationCode.LOGIN_ERROR.getCode(),ApplicationCode.LOGIN_ERROR.getMessage()));
        } else {
            return JSON.toJSONString(new Message(ApplicationCode.LOGIN_SUCCESS.getCode(),ApplicationCode.LOGIN_SUCCESS.getMessage()));
        }
    }

    private String loginUser(SysUser user) {
        if (isRelogin(user)) return ApplicationCode.LOGIN_SUCCESS.getCode(); // 如果已经登陆，无需重新登录
        return shiroLogin(user); // 调用shiro的登陆验证
    }

    private String shiroLogin(SysUser user) {
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getLoginpass().toCharArray(), null);
        token.setRememberMe(true);

        // shiro登陆验证
        try {
            SecurityUtils.getSubject().login(token);
        } catch (UnknownAccountException ex) {
            return ApplicationCode.LOGIN_ERROR.getCode();
        } catch (IncorrectCredentialsException ex) {
            return ApplicationCode.LOGIN_ERROR.getCode();
        } catch (AuthenticationException ex) {
            return ex.getMessage(); // 自定义报错信息
        } catch (Exception ex) {
            ex.printStackTrace();
            return ApplicationCode.ERROR.getCode();
        }
        return ApplicationCode.LOGIN_SUCCESS.getCode();
    }

    private boolean isRelogin(SysUser user) {
        Subject us = SecurityUtils.getSubject();
        if (us.isAuthenticated()) {
            return true; // 参数未改变，无需重新登录，默认为已经登录成功
        }
        return false; // 需要重新登陆
    }

    @RequiresPermissions("console")
    @RequestMapping("/console.action")
    public String console() {
        return "site/console";
    }

    @RequestMapping("/config.action")
    public String config() {
        return "site/config";
    }



}
